package mediator

import (
	"fmt"
	"strings"
)

type CDDriver struct {
	Data string
}

func (c *CDDriver) ReadData() {
	fmt.Println("CDDriver read:", c.Data)
}

type CPUDriver struct {
	Video string
	Sound string
}

func (c *CPUDriver) Process(data string) {
	sp := strings.Split(data, ",")
	c.Sound = sp[0]
	c.Video = sp[1]
	fmt.Println("CPU play Sound:", c.Sound, " play Video:", c.Video)
}

type GPUDriver struct {
	Image string
}

func (g *GPUDriver) Display(data string) {
	g.Image = data
	fmt.Println("GPU show image:", g.Image)
}

type Mediator struct {
	CD  *CDDriver
	GPU *GPUDriver
	CPU *CPUDriver
}

var mediator *Mediator

func GetMediatorInstance() *Mediator {
	if mediator == nil {
		mediator = &Mediator{}
	}
	return mediator
}

func (m *Mediator) change(i interface{}) {
	switch inst := i.(type) {
	case *CDDriver:
		m.CD.ReadData()
	case *CPUDriver:
		m.CPU.Process(inst.Sound + "," + inst.Video)
	case *GPUDriver:
		m.GPU.Display(inst.Image)
	default:
	}
}
